package ltd.newbee.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ltd.newbee.mall.api.mall.vo.NewBeeMallUserAddressVO;
import ltd.newbee.mall.common.NewBeeMallException;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.MallUserAddress;
import ltd.newbee.mall.mapper.MallUserAddressMapper;
import ltd.newbee.mall.service.NewBeeMallUserAddressService;
import ltd.newbee.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewBeeMallUserAddressServiceImpl implements NewBeeMallUserAddressService {
    @Autowired
    private MallUserAddressMapper userAddressMapper;

    @Override
    public List<NewBeeMallUserAddressVO> getMyAddresses(Long userId) {
        LambdaQueryWrapper<MallUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MallUserAddress::getUserId, userId)
                .eq(MallUserAddress::getIsDeleted, 0)
                .orderByDesc(MallUserAddress::getAddressId);
        List<MallUserAddress> myAddressList = userAddressMapper.selectList(queryWrapper);
        return BeanUtil.copyList(myAddressList, NewBeeMallUserAddressVO.class);
    }

    @Override
    public Boolean saveUserAddress(MallUserAddress mallUserAddress) {
        if (mallUserAddress.getDefaultFlag().intValue() == 1) {
            //添加默认地址，需要将原有的默认地址修改掉
            MallUserAddress defaultAddress = getMyDefaultAddressByUserId(mallUserAddress.getUserId());
            if (defaultAddress != null) {
                Date now = new Date();
                LambdaUpdateWrapper<MallUserAddress> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper
                        .set(MallUserAddress::getDefaultFlag, 0)
                        .set(MallUserAddress::getUpdateTime, now)
                        .eq(MallUserAddress::getAddressId, defaultAddress.getAddressId());
                if (userAddressMapper.update(null, updateWrapper) < 1) {
                    //未更新成功
                    NewBeeMallException.fail(ServiceResultEnum.DB_ERROR.getResult());
                }
            }
        }
        return userAddressMapper.insert(mallUserAddress) > 0;
    }

    @Override
    public Boolean updateMallUserAddress(MallUserAddress mallUserAddress, Long userId) {
        MallUserAddress tempAddress = getMallUserAddressById(mallUserAddress.getAddressId());
        Date now = new Date();
        if (mallUserAddress.getDefaultFlag().intValue() == 1) {
            //添加默认地址，需要将原有的默认地址修改掉
            MallUserAddress defaultAddress = getMyDefaultAddressByUserId(mallUserAddress.getUserId());
            if (defaultAddress != null && !defaultAddress.getAddressId().equals(tempAddress.getAddressId())) {
                LambdaUpdateWrapper<MallUserAddress> updateWrapper = Wrappers.lambdaUpdate(MallUserAddress.class);
                updateWrapper
                        .set(MallUserAddress::getDefaultFlag, 0)
                        .set(MallUserAddress::getUpdateTime, now)
                        .eq(MallUserAddress::getUserId, userId)
                        .eq(MallUserAddress::getAddressId, defaultAddress.getAddressId());
                if (userAddressMapper.update(null, updateWrapper) < 1) {
                    //未更新成功
                    NewBeeMallException.fail(ServiceResultEnum.DB_ERROR.getResult());
                }
            }
        }
        LambdaQueryWrapper<MallUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MallUserAddress::getAddressId, mallUserAddress.getAddressId());
        mallUserAddress.setUpdateTime(now);
        return userAddressMapper.update(mallUserAddress, queryWrapper) > 0;
    }

    @Override
    public MallUserAddress getMallUserAddressById(Long addressId) {
        LambdaQueryWrapper<MallUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MallUserAddress::getAddressId, addressId)
                .eq(MallUserAddress::getIsDeleted, 0);
        return userAddressMapper.selectOne(queryWrapper);
    }

    @Override
    public MallUserAddress getMyDefaultAddressByUserId(Long userId) {
        LambdaQueryWrapper<MallUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MallUserAddress::getUserId, userId)
                .eq(MallUserAddress::getDefaultFlag, 1)
                .eq(MallUserAddress::getIsDeleted, 0);
        return userAddressMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean deleteById(Long addressId) {
        LambdaUpdateWrapper<MallUserAddress> updateWrapper = Wrappers.lambdaUpdate(MallUserAddress.class);
        updateWrapper
                .set(MallUserAddress::getIsDeleted, 1)
                .eq(MallUserAddress::getAddressId, addressId);
        return userAddressMapper.update(null, updateWrapper) > 0;
    }
}
